//
//  NETAPIClient.m
//  RequestNET
//
//  Created by gavin on 14-8-12.
//  Copyright (c) 2014年 gavin.gu@live.com. All rights reserved.
//

#import "NETAPIClient.h"

static NETAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;

@implementation NETAPIClient


+ (instancetype)sharedClient {
    
    [NETAPIClient onceTokenMethod];
    if (!_sharedClient.reachabilityManager.reachable) {
        NSLog(@"网络连接失败");
    }
    return _sharedClient;
}

+ (instancetype)sharedClientWithoutNetworkChecking {
    
    [NETAPIClient onceTokenMethod];
    return _sharedClient;
}

+ (void)reset {
    _sharedClient = nil;
    onceToken = 0;
}

+ (void)onceTokenMethod
{
    NSString * VWAPIBaseURLString = [NSString stringWithFormat:SERVER_INTERFACES,@""];//SERVER_IP
    
    dispatch_once(&onceToken, ^{
        _sharedClient = [[NETAPIClient alloc] initWithBaseURL:[NSURL URLWithString:VWAPIBaseURLString]];
        [_sharedClient setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]];
        
        [_sharedClient.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    NSLog(@"网络已连接");
                    break;
                    
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    NSLog(@"网络已连接");
                    break;
                    
                case AFNetworkReachabilityStatusNotReachable:
                    NSLog(@"网络连接失败");
                    break;
                    
                default:
                    break;
            }
        }];
        [_sharedClient.reachabilityManager startMonitoring];
    });
}


@end
